﻿using AutoMapper;
using MediatR;
using RedDragon.Common.Domain.MaterialManagements;
using RedDragon.Common.Domain.ProductionTasks;
using RedDragon.Common.Dtos;
using RedDragon.Common.Infrastructure.Interface;
using RedDragon.ERP.ErroCodeEnum;
using SqlSugar;
using T.Common.Domain.Common;

namespace RedDragon.ERP.API.Application.Commands.ProductionTasks
{
    /// <summary>
    /// 生产任务列表Handler
    /// </summary>
    public class SearchProductionTasksHandler : IRequestHandler<SearchProductionTasksCommand, Result<List<ProductionTasksDto>>>
    {
        private readonly ISqlSugarClient _db;
        /// <summary>
        /// mapper
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 生产任务表仓储
        /// </summary>
        public readonly IBaseRepository<Productiontasks> productiontasksrepository;
        /// <summary>
        /// 任务类型表仓储
        /// </summary>
        public readonly IBaseRepository<TaskType> taskTyperepository;
        /// <summary>
        /// 物料管理表仓储
        /// </summary>
        public readonly IBaseRepository<MaterialManagement> materialManagementrepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<SearchProductionTasksHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db"></param>
        /// <param name="mapper"></param>
        /// <param name="productiontasksrepository"></param>
        /// <param name="taskTyperepository"></param>
        /// <param name="logger"></param>
        /// <param name="materialManagementrepository"></param>
        public SearchProductionTasksHandler(ISqlSugarClient db, IMapper mapper, IBaseRepository<Productiontasks> productiontasksrepository, IBaseRepository<TaskType> taskTyperepository, ILogger<SearchProductionTasksHandler> logger, IBaseRepository<MaterialManagement> materialManagementrepository)
        {
            _db = db;
            this.mapper = mapper;
            this.productiontasksrepository = productiontasksrepository;
            this.taskTyperepository = taskTyperepository;
            this.logger = logger;
            this.materialManagementrepository = materialManagementrepository;
        }
        /// <summary>
        /// 生产任务列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result<List<ProductionTasksDto>>> Handle(SearchProductionTasksCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<List<ProductionTasksDto>>() { Code = (int)CommonErrorCode.Success };
            try
            {
                var query = await _db.Queryable<Productiontasks, TaskType, MaterialManagement>((a, b,c) => new JoinQueryInfos(
                    JoinType.Inner, a.TaskTypeId == b.TaskTypeId,
                    JoinType.Inner, a.MaterialId == c.MaterialId
                    ))
                    .Select((a, b,c) => new ProductionTasksDto()
                    {
                        ProductiontasksId = a.ProductiontasksId,    
                        ProductiontasksCode=a.ProductiontasksCode,
                        ProductiontasksName=a.ProductiontasksName,
                        ProductionQuantity=a.ProductionQuantity,
                        StartTime = a.StartTime,
                        EndTime=a.EndTime,
                        IsStatus=a.IsStatus,
                        MaterialId=a.MaterialId,
                        MaterialName=c.MaterialName,
                        MaterialCode=c.MaterialCode,
                        Specifications=c.Specifications,
                        TaskTypeId=a.TaskTypeId,
                        TaskTypeName=b.TaskTypeName,
                        IsDel = a.IsDel,
                        AuditStatus = a.AuditStatus,
                        CreatedOwner = a.CreatedOwner,
                        CreatedTime = a.CreatedTime
                    }).Where(a => a.IsDel == 1).ToListAsync();
                result.Data = query;
                result.Msg = "生产任务显示查询成功";
            }
            catch (Exception ex)
            {
                result.Msg = "生产任务显示查询失败" + ex.Message;
                logger.LogError("生产任务显示查询失败" + ex.Message);
                throw;
            }
            return result;
        }
    }
}
